package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.Utils;

import java.text.DecimalFormat;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.manager.TimeRefresher;

public class MiaoBiaoActivity extends BaseActivity implements TimeRefresher.OnTimeRefreshListener, View.OnClickListener {
    private final String TAG = "MiaoBiaoActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, MiaoBiaoActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.miaobiao_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private TextView bt_begin;
    private TextView bt_reset;
    private TextView tv_time;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        bt_begin = (TextView) findViewById(R.id.bt_begin);
        bt_reset = (TextView) findViewById(R.id.bt_reset);
        tv_time = (TextView) findViewById(R.id.tv_time);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    @Override
    public void initData() {//必须在onCreate方法内调用

    }


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        bt_begin.setOnClickListener(this);
        bt_reset.setOnClickListener(this);
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_begin:
                onBegin();
                break;
            case R.id.bt_reset:
                onReset();
                break;
            default:
                break;
        }
    }

    boolean _isWorking = false;

    private void onBegin() {
        if (_isWorking) {
            _isWorking = false;
            bt_begin.setText("开始");
        } else {
            bt_begin.setText("停止");
            _isWorking = true;
        }
    }

    private void onReset() {
        _count = 0;
        refresh();
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }
        _isFirst = false;
        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
        findViewById(R.id.bt_begin).requestFocus();
    }

    private int[] CTRL_IDS = {
            R.id.bt_begin, R.id.bt_reset};

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    @Override
    protected void onResume() {
        super.onResume();
        TimeRefresher.getInstance().addTimeRefreshListener(TAG, 10, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        TimeRefresher.getInstance().removeTimeRefreshListener(TAG);
    }

    @Override
    public void onTimerStart() {

    }

    @Override
    public void onTimerRefresh() {
        if (!_isWorking) {
            return;
        }
        _count++;
        refresh();
    }

    int _count = 0;//10 ms

    private void refresh() {
        String strC = getTimeDF(_count);
        tv_time.setText(strC);
    }

    String getTimeDF(int secondtime) {
        if (secondtime < 0) {
            secondtime = 0;
        }
        DecimalFormat DF = new DecimalFormat("00");
        int second = secondtime / 100;
        int ms = secondtime % 100;
        String strTime = DF.format(second) + ":" + DF.format(ms);
        return strTime;
    }

    @Override
    public void onTimerStop() {

    }


}
